package com.yanqu.road.dao.impl.log;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.log.LogUserInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LogUserInfoDaoImpl extends TempDao {

	public boolean addLogUserInfoList(List<LogUserInfo> list) {
		boolean result = false;
		getDb().setConnName(DbNameStrategy.getServerDb());
		String sql = "INSERT INTO t_s_user_list (openId,serverId,userId,nickName,registerTime,mac,ip,phonePlatform,originalServerId)"
				+ " VALUES (?,?,?,?,?,?,?,?,?)";

		List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
		for (LogUserInfo info : list) {
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.NVARCHAR, info.getOpenId()));
			para.put(i++, new DbParameter(Types.BIGINT, info.getServerId()));
			para.put(i++, new DbParameter(Types.BIGINT, info.getUserId()));
			para.put(i++, new DbParameter(Types.NVARCHAR, info.getNickName()));
			para.put(i++, new DbParameter(Types.BIGINT, info.getRegisterTime()));

			para.put(i++, new DbParameter(Types.NVARCHAR, info.getMac()));
			para.put(i++, new DbParameter(Types.NVARCHAR, info.getIp()));
			para.put(i++, new DbParameter(Types.INTEGER, info.getPhonePlatform()));
			para.put(i++, new DbParameter(Types.BIGINT, info.getOriginalServerId()));
			mapList.add(para);
		}

		if (getDb().executeBatchQuery(sql, mapList) > -1) {
			result = true;
		}
		return result;
	}

	public List<Long> getUserServerListByOpenId(String openId) {
		List<Long> list = new ArrayList<>();
		String sql = "select originalServerId from  t_s_user_list where openId = ? ";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, openId);
			rs = call.executeQuery();
			while (rs.next()) {
				list.add(rs.getLong("originalServerId"));
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return list;
	}

	public Map<Long, Long> getUserServerListRegisterTime(String openId) {
		Map<Long, Long> dataMap = new HashMap<>();
		String sql = "select originalServerId,registerTime from  t_s_user_list where openId = ? ";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, openId);
			rs = call.executeQuery();
			while (rs.next()) {
				long originalServerId = rs.getLong("originalServerId");
				long registerTime = rs.getLong("registerTime");
				if (!dataMap.containsKey(originalServerId)) {
					dataMap.put(originalServerId,registerTime);
				} else if (dataMap.get(originalServerId) > registerTime) {
					dataMap.put(originalServerId,registerTime);
				}
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public List<LogUserInfo> getUserServerListByOpenId(List<Long> userIdList) {
		List<LogUserInfo> list = new ArrayList<>();
		String sql = "select * from t_s_user_list where userId in (" + StringUtils.listToString(userIdList, ",") + ")";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				LogUserInfo logUserInfo = parseLogUserInfo(rs);
				list.add(logUserInfo);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return list;
	}

	private LogUserInfo parseLogUserInfo(ResultSet rs) throws SQLException {
		String openId = rs.getString("openId");
		long serverId = rs.getLong("serverId");
		long userId = rs.getLong("userId");
		String nickName = rs.getString("nickName");
		long registerTime = rs.getLong("registerTime");
		String mac = rs.getString("mac");
		String ip = rs.getString("ip");
		int phonePlatform = rs.getInt("phonePlatform");
		long originalServerId = rs.getLong("originalServerId");

		if (openId == null) openId = "";
		if (nickName == null) nickName = "";
		if (mac == null) mac = "";
		if (ip == null) ip = "";

		return new LogUserInfo(serverId, userId, openId, nickName, registerTime, mac, ip, phonePlatform, originalServerId);
	}

}
